<?php 
class Db{
    private $host;
    private $root;
    private $pwd;
    private $dbname;
    private $port;
    private $charset;
    public $link;//数据库对象
    public function __construct($params)
    {
        //初始化属性
        $this->init($params);
        $this->connect();

        //创建数据库对象


    }
    private function init($params=array()){
        $this->host=$partams['host']??'localhost';  //isset($params['host])?$params['host']:'localhost'
        $this->root=$params['root']??'root';
        $this->pwd=$params['pwd']??'123';

        $this->dbname=$params['dbname'];
        $this->port=$params['port']??'8080';
        $this->charset=$params['charset']??'utf8';
    }
//创建数据库对象
private function connect(){
    $this->link=@mysqli_connect($this->host,$this->root,$this->pwd,$this->dbname,$this->port);
    if(!$this->link){
        echo "数据库连接失败".mysqli_connect_error();
        exit;
    }
    mysqli_set_charset($this->link,$this->charset);


}
//执行增删改查
public function execute($sql){
    if(!$str=mysqli_query($this->link,$sql)){
        echo "sql执行错误".mysqli_error($this->link);
        echo "<br>错误的sql语句是".$sql;
        exit;
    }
    return $str;
}
//查询
public function select($sql,$type="MYSQLI_ASSOC"){
    $result=$this->execute($sql);
    $type=$this->getType($type);
    if((mysqli_num_rows($result)>0)){
     return mysqli_fetch_all($result,MYSQLI_BOTH);
    }


}
public function getType($type){
if($type=='asso'){
    $type="MYSQLI_ASSOC";
}else if($type=='num'){
    $type="MYSQLI_NUM";
}else{
    $type="MYSQLI_BOTH";
}
return $type;
}

}
$str=[
"host"=>"127.0.0.1",
"root"=>"root",
"pwd"=>"root",
"dbname"=>"db"
];

$sql="insert into user values(null,'账上','10')";
$obj=new Db($str);
// var_dump($obj);
$obj->execute($sql);
var_dump($obj->execute($sql));
?>